﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

using EncodingConvertor.Source;

namespace EncodingConvertor
{
    public partial class frmConfig : Form
    {
        string keyPath = @"SoftWare\StFileEncodingConvertor";

        public frmConfig()
        {
            InitializeComponent();
        }

        private void frmConfig_Load(object sender, EventArgs e)
        {
            this.panel1.Enabled = false;

            try
            {
                Config.LoadConfig();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }

            this.txtFileFilter.Text = Config.FILEFILTER;

            if (Config.SOURCEENCODING == -1)
            {
                this.chkUnknownEncoding.Checked = true;
                this.cmbTargetEncode.SelectedIndex = Config.TARGETENCODING;
            }
            else
            {
                this.chkUnknownEncoding.Checked = false;
                this.cmbSourceEncode.SelectedIndex = Config.SOURCEENCODING;
                this.cmbTargetEncode.SelectedIndex = Config.TARGETENCODING;
            }

            if (Config.BACKUPPATH.Equals(""))
            {
                this.chkIsBackup.Checked = false;
            }
            else
            {
                this.chkIsBackup.Checked = true;
                this.txtFilePath.Text = Config.BACKUPPATH;
            }
        }

        private void chkIsBackup_CheckedChanged(object sender, EventArgs e)
        {
            this.panel1.Enabled = chkIsBackup.Checked;
        }

        private void chkUnknownEncoding_CheckedChanged(object sender, EventArgs e)
        {
            cmbSourceEncode.Enabled = !chkUnknownEncoding.Checked;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                RegistryKey RegKey;
                RegKey = Registry.LocalMachine.OpenSubKey(keyPath, true);

                if (RegKey == null)
                {
                    RegKey = Registry.LocalMachine.CreateSubKey(keyPath);
                }

                RegKey.SetValue("FileFilter", this.txtFileFilter.Text.Trim());

                if (!this.chkUnknownEncoding.Checked)
                {
                    RegKey.SetValue("SourceEncoding", this.cmbSourceEncode.SelectedIndex);
                }
                else
                {
                    RegKey.SetValue("SourceEncoding", -1);
                }

                RegKey.SetValue("TargetEncoding", this.cmbTargetEncode.SelectedIndex);
                RegKey.SetValue("ShowAD", Convert.ToInt32(this.chkShowAD.Checked));

                if (this.chkIsBackup.Checked)
                {
                    RegKey.SetValue("BakupPath", this.txtFilePath.Text.Trim());
                }
                else
                {
                    RegKey.SetValue("BakupPath","");
                }
                MessageBox.Show("保存配置成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Config.LoadConfig();
                this.Close();
            }
        }

        private void btnBrowser_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);

            if (fbd.ShowDialog() == DialogResult.OK)
            {
                this.txtFilePath.Text = fbd.SelectedPath;
            }
        }
    }
}